# Import the required libraries
from selenium import webdriver
from browsermobproxy import Server
import time

# Start the Browsermob Proxy Server
server = Server(
    r"D:\browsermob-proxy-2.1.4-bin\browsermob-proxy-2.1.4\bin\browsermob-proxy")
server.start()
proxy = server.create_proxy()


# Use the Browsermob Proxy with Selenium
# chrome_options = webdriver.FirefoxOptions()
# chrome_options.add_argument("--disable-dev-shm-usage")
# chrome_options.add_argument("--no-sandbox")
# chrome_options.add_argument('--ignore-certificate-errors')
# chrome_options.add_argument("--proxy-server={0}".format(proxy.proxy))
# driver = webdriver.Firefox(
#     executable_path=r"D:\geckodriver.exe", options=chrome_options)


selenium_proxy = proxy.selenium_proxy()
caps = webdriver.DesiredCapabilities.FIREFOX
caps['marionette'] = True

proxy_settings = {
    "proxyType": "MANUAL",
    "httpProxy": selenium_proxy.httpProxy,
    "sslProxy": selenium_proxy.sslProxy,
}
caps['proxy'] = proxy_settings

driver = webdriver.Firefox(executable_path=r"D:\geckodriver.exe",desired_capabilities=caps)
# Start capturing traffic
proxy.new_har("chatGPT", options={
    'captureContent': True,
    'captureHeaders': True
})

# Open a webpage using Selenium
driver.get("https://www.baidu.com")
driver.get("https://www.baidu.com")

har = proxy.har
server.stop()
driver.quit()
print(har)

if __name__ == '__main__':
    ...
